/**
 * 项目名称: Screeps Arena code
 * 创建时间: 2023/9/27
 * 作者: 码力全开
 * 基地类 每次实例化都会创建一个基地类
 */
import {getObjectsByPrototype} from "game/utils";
import {Creep, StructureSpawn, Source, StructureContainer} from "game/prototypes";
import {ATTACK, BodyPartConstant, CARRY, ERR_NOT_IN_RANGE, HEAL, MOVE, RESOURCE_ENERGY, WORK} from "game/constants";
import {MyCreep} from "./myCreep.mjs";
import Room from "./room.mjs";
import {army} from "../types/army.mjs";



export class Fort extends Room {
    get ATTACK_PEOPLE(): number {
        return this._ATTACK_PEOPLE;
    }
    set ATTACK_PEOPLE(value: number) {
        this._ATTACK_PEOPLE = value;
    }
    get GATHER_PEOPLE(): number {
        return this._GATHER_PEOPLE;
    }

    set GATHER_PEOPLE(value: number) {
        this._GATHER_PEOPLE = value;
    }

    /**
     * 攻击兵种数量
     */
    private _ATTACK_PEOPLE: number = 0
    /**
     * 预计攻击兵种人数
     * @private
     */
    private TARGET_ATTACK_PEOPLE = 1
    /**
     * 采集兵种数量
     */
    private _GATHER_PEOPLE: number = 0
    /**
     * 预计采集人数
     * @private
     */
    private TARGET_GATHER_PEOPLE_NUMBER = 1


    /**
     * 攻击类型和治疗小兵的阵容组成
     * 每种类型只需要一种即可，不要存在重复的
     * 否则会造成统计错误
     * @private
     */
    private ATTACK_TYPE: army[] = [
        {
            type: [MOVE, MOVE, ATTACK, ATTACK, ATTACK],
            weight: 1,
            current: 0,
            plan_number: 2,
            is_success:false
        },
        {
            type: [MOVE, MOVE, ATTACK, ATTACK, ATTACK,HEAL],
            weight: 1,
            current: 0,
            plan_number: 2,
            is_success:false

        },
        {
            type: [MOVE, MOVE, ATTACK, HEAL, HEAL],
            weight: 1,
            current: 0,
            plan_number: 1,
            is_success:false

        },
    ]
    /**
     * 我的军队
     * @private
     */
    private myCreep: MyCreep = new MyCreep()


    /**
     * ATTACK_PEOPLE 攻击人数
     * GATHER_PEOPLE 采集人数
     */
    constructor(ATTACK_PEOPLE: number = 5, GATHER_PEOPLE: number = 1) {
        super()
        this.TARGET_ATTACK_PEOPLE = ATTACK_PEOPLE
        this.TARGET_GATHER_PEOPLE_NUMBER = GATHER_PEOPLE
        this.FindFort()
        this.Gather()
        // this.FindSource()
    }

    public Tick() {
        this.check_count++
        this.myCreep.startCreate()
        // console.log("检查采集状态",this.check_count)
        if (!this.MY_Structure_Spawn) this.FindFort()
        this.Gather()
        this.updateEnergy()
        this.myCreep.watch()
        if (this.check_count > 150 && this.GATHER_PEOPLE > 2) {
            this.myCreep.startCrateCreep(this.ATTACK_TYPE)
        }
        // 在游戏进行到 xxx 的时候就进行向敌方基地进行攻击
        if (this.check_count<1000){
            this.myCreep.rallyPoint({
                x:5,
                y:35
            })
        }


        // this.myCreep.defense()
        this.checkCreep(this.ATTACK_TYPE)

        this.selfDefense()
    }


    /**
     *
     * @Gather 采集函数
     */
    public Gather() {
        const cather_number: number = this.GATHER_PEOPLE
        if (cather_number < 3 && this.energy > 500) {
            this.GATHER_PEOPLE = this.myCreep.CreateGather(1, cather_number)
        } else {
            const worker = getObjectsByPrototype(Creep).filter(item => item.my).filter(item => item.body.some(i => i.type == WORK))
            if (!this.MY_Structure_Spawn) return
            for (let i = 0; i < worker.length; i++) {
                let source = this.FindSource().find(item=>item.store.energy > 0)
                this.gatherStructureContainer(worker[i], source, this.MY_Structure_Spawn)
            }
        }
    }

    /**
     * 采集固定资源的方法
     * @param creep 载具
     * @param source 资源对象
     * @param home 转移目标
     */
    gatherStructureContainer(creep: Creep, source: StructureContainer | undefined, home: StructureSpawn) {
        if (!source) {
            return
        }
        if (creep.store[RESOURCE_ENERGY] < (creep.store.getCapacity() || 0)) {
            if (creep.withdraw(source, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
                creep.moveTo(source);
            }
        } else {
            if (creep.transfer(home, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
                creep.moveTo(home);
            }
        }
    }

    /**
     * 采集散装资源的方法
     * @param creep 载具
     * @param source 资源对象
     * @param home 转移目标
     */
    gatherResource(creep: Creep, source: Source, home: StructureSpawn) {
        if (creep.store[RESOURCE_ENERGY] < (creep.store.getCapacity() || 0)) {
            if (creep.harvest(source) == ERR_NOT_IN_RANGE) {
                creep.moveTo(source);
            }
        } else {
            if (creep.transfer(home, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
                creep.moveTo(home);
            }
        }

    }


}
